package com.carsys.controller;

import com.carsys.dto.ActionResult;
import com.carsys.entity.SUser;
import com.carsys.service.SUserService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/carsys/user")
public class SUserController {

    @Autowired
    private SUserService sUserService;

    /**
     * 登录的请求
     * @param tel
     * @param password
     * @return
     */
    @RequestMapping(value="/login",method = RequestMethod.POST)
    public ActionResult login(String tel, String password, HttpSession session){
        SUser user = sUserService.findSUserByTelAndPassword(tel,password);
        if(user!=null){
            ActionResult result = new ActionResult(200, null, user);
            session.setAttribute("user",user);
            return result;
        }
        return null;

    }

    /**
     * 注册的请求
     * @param user
     * @return
     */
    @RequestMapping(value="/register",method = RequestMethod.POST)
    public ActionResult register(SUser user){
        if(sUserService.insertSUser(user)){
            ActionResult result = new ActionResult(200, null, user);
            return result;
        }
        return null;
    }

    /**
     * 找回密码
     * @param tel
     * @param email
     * @return
     */
    @RequestMapping(value="/findPassword",method = RequestMethod.POST)
    public ActionResult findPassword(String tel,String email){

        if(sUserService.findSUserByTelAndEmail(tel, email)){
            String msg = "您的密码已经发送到您的邮箱了";
            ActionResult result = new ActionResult(200, msg, null);
            return result;
        }
        return null;
    }

    @RequestMapping(value="/getUser",method = RequestMethod.GET)
    public ActionResult getUser(int id){
        SUser paramUser = new SUser();
        paramUser.setId(id);
        SUser user = sUserService.findSUserOne(paramUser);
        if(user!=null){
            ActionResult result = new ActionResult(200,null,user);
            return result;
        }
        return null;
    }

    /**
     *  修改用户
     * @param user
     * @return
     */
    @RequestMapping(value = "/updateUser",method = RequestMethod.PUT)
    public ActionResult updateUser(@RequestBody SUser user){
        if(sUserService.updateUser(user)){
            String msg = "修改成功";
            ActionResult result = new ActionResult(200, msg, null);
            return result;
        }
        return null;
    }

    /**
     *  用户禁用
     * @param id
     * @return
     */
    @RequestMapping(value ="/disable",method = RequestMethod.GET)
    public ActionResult disable(int id){
        if(sUserService.updateIdentityById(id,2)){
            String msg = "禁用成功";
            ActionResult result = new ActionResult(200, msg, null);
            return result;
        }
        return null;
    }

    /**
     *  用户禁用
     * @param id
     * @return
     */
    @RequestMapping(value ="/unlock",method = RequestMethod.GET)
    public ActionResult unlock(int id){
        if(sUserService.updateIdentityById(id,1)){
            String msg = "解除禁用成功";
            ActionResult result = new ActionResult(200, msg, null);
            return result;
        }
        return null;
    }

    @RequestMapping(value ="/delUser",method = RequestMethod.GET)
    public ActionResult delUser(int id){
        if(sUserService.deleteUserById(id)){
            String msg = "删除成功";
            ActionResult result = new ActionResult(200, msg, null);
            return result;
        }
        return null;
    }

    /**
     *  查找列表所有普通用户
     * @return
     */
    @RequestMapping(value="/findAllUser",method = RequestMethod.GET)
    public ActionResult findAllUser(){
        List<SUser> userList = sUserService.findAllUser(1);
        if(userList!=null){
            ActionResult result = new ActionResult(200, null, userList);
            return result;
        }
        return null;
    }


    @RequestMapping(value="/findAllDisableUser",method = RequestMethod.GET)
    public ActionResult findAllDisableUser(){
        List<SUser> userList = sUserService.findAllUser(2);
        if(userList!=null){
            ActionResult result = new ActionResult(200, null, userList);
            return result;
        }
        return null;
    }



    @RequestMapping(value = "/update.password", method = RequestMethod.POST)
    public ActionResult updateSUserPassword(String password, int id){

        if(sUserService.updateSUserPasswordById(id,password)){
            ActionResult result = new ActionResult(200,null,null);
            return result;
        }else{
            ActionResult result = new ActionResult(403,null,null);
            return result;
        }
    }

    @RequestMapping(value = "/denglu.do",method = RequestMethod.GET)
    @ResponseBody
    public ActionResult showLoginSUser(SUser sUser){
//        System.out.println(sUser);
        int id = sUser.getId();
        SUser user = sUserService.findSUserById(id);
        ActionResult result = new ActionResult(200,null,new Gson().toJson(user));
        return result;
    }

    @RequestMapping(value = "/update.tel", method = RequestMethod.POST)
    public ActionResult updateSUserTel(String tel, int id){

        if(sUserService.updateSUserTelById(id,tel)){
            ActionResult result = new ActionResult(200,null,null);
            return result;
        }else{
            ActionResult result = new ActionResult(403,null,null);
            return result;
        }
    }

    @RequestMapping(value = "/update.email", method = RequestMethod.POST)
    public ActionResult updateSUserEmail(String email, int id){

        if(sUserService.updateSUserEmailById(id,email)){
            ActionResult result = new ActionResult(200,null,null);
            return result;
        }else{
            ActionResult result = new ActionResult(403,null,null);
            return result;
        }
    }



}
